import 'package:flutter/material.dart';
import './client_dio.dart';
import './item_args.dart';

// A Widget that extracts the necessary arguments from
// the ModalRoute.
class ItemArgumentsScreen extends StatelessWidget {
  const ItemArgumentsScreen({super.key});

  static const routeName = '/extractItemArguments';

  @override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute
    // settings and cast them as ItemArguments.
    final args = ModalRoute.of(context)!.settings.arguments as ItemArguments;
    final TextEditingController name_Controller = new TextEditingController(text: args.name);
    final TextEditingController guige_Controller = new TextEditingController(text: args.guige);
    final TextEditingController danwei_Controller = new TextEditingController(text: args.danwei);
    return Scaffold(
      appBar: AppBar(
        title: Text("Edit Item"),
      ),
      body: Center(
        child: 
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(controller:  name_Controller,
              decoration: InputDecoration(labelText: "Name"),
              ),
            TextField(controller:  guige_Controller,
              decoration: InputDecoration(labelText: "规格"),
              ),
            TextField(controller:  danwei_Controller,
              decoration: InputDecoration(labelText: "单位"),
              ),
            ElevatedButton(
            onPressed: (){
              put("/rest/Item/", {"id":args.id,"name":name_Controller.text, "guige": guige_Controller.text, "danwei": danwei_Controller.text},(r){
                  print(r);
                  Navigator.of(context).pop(true); // Return true to indicate success
                }, (e){
                  print(e);
                  Navigator.of(context).pop(false); // Return false to indicate failure
                });
            }, child: Text("Save")),
            ElevatedButton(
            onPressed: (){
              Navigator.of(context).pop();
            }, child: Text("Cancel"))
            ]
      )),);
  }
}

// A widget for creating a new item
class CreateItemScreen extends StatelessWidget {
  const CreateItemScreen({super.key});

  static const routeName = '/createItem';

  @override
  Widget build(BuildContext context) {
    final TextEditingController name_Controller = new TextEditingController(text: "");
    final TextEditingController bh_Controller = new TextEditingController(text: "");
    final TextEditingController guige_Controller = new TextEditingController(text: "");
    final TextEditingController danwei_Controller = new TextEditingController(text: "");
    return Scaffold(
      appBar: AppBar(
        title: Text("Create New Item"),
      ),
      body: Center(
        child: 
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(controller:  bh_Controller,
              decoration: InputDecoration(labelText: "BH"),
              ),
            TextField(controller:  name_Controller,
              decoration: InputDecoration(labelText: "Name"),
              ),
            TextField(controller:  guige_Controller,
              decoration: InputDecoration(labelText: "规格"),
              ),
            TextField(controller:  danwei_Controller,
              decoration: InputDecoration(labelText: "单位"),
              ),
            ElevatedButton(
            onPressed: (){
              post("/rest/Item/", {"bh":bh_Controller.text,"name":name_Controller.text, "guige": guige_Controller.text, "danwei": danwei_Controller.text},(r){
                print(r);
                Navigator.of(context).pop(true); // Return true to indicate success
              }, (e){
                print(e);
                Navigator.of(context).pop(false); // Return false to indicate failure
              });
            }, child: Text("Save")),
            ElevatedButton(
            onPressed: (){
              Navigator.of(context).pop();
            }, child: Text("Cancel"))
            ]
      )),);
  }
}

// A Widget that accepts the necessary arguments via the
// constructor.
class PassItemArgumentsScreen extends StatelessWidget {
  static const routeName = '/passItemArguments';

  final int id;
  final String name;

  // This Widget accepts the arguments as constructor
  // parameters. It does not extract the arguments from
  // the ModalRoute.
  //
  // The arguments are extracted by the onGenerateRoute
  // function provided to the MaterialApp widget.
  const PassItemArgumentsScreen({
    super.key,
    required this.id,
    required this.name,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(id.toString()),
      ),
      body: Center(
        child: Text(name),
      ),
    );
  }
}